package com.smartpower.wattmeter.ui;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.android.basiclib.base.BaseActivity;
import com.android.basiclib.uitls.CheckUtil;
import com.android.basiclib.uitls.CommUtils;
import com.android.basiclib.uitls.NetWorkUtil;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.smartpower.wattmeter.Constants;
import com.smartpower.wattmeter.R;
import com.smartpower.wattmeter.bean.EditBean;

/**
 * 编辑设备的信息，详情跳转
 */
public class DeviceEditActivity extends BaseActivity {

    private com.android.basiclib.view.titlebar.EasyTitleBar mEasyTitle;
    private android.widget.EditText mETContent;
    private android.widget.TextView mTvSave;
    private int mType;
    private String mText;

    public static void startInstance(int type, String text) {
        Context context = CommUtils.getContext();
        Intent intent = new Intent(context, DeviceEditActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("type", type);
        intent.putExtra("text", text);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_divice_eidt;
    }

    @Override
    protected void getDataFromIntent(Intent intent) {
        mType = intent.getIntExtra("type", 0);
        mText = intent.getStringExtra("text");
    }

    @Override
    protected void init() {
        initView();
        initData();
        initListener();
    }

    private void initData() {
        String title = "";
        if (mType == 1) {
            title = "计量资产条形码ID";
        } else if (mType == 2) {
            title = "计量资产类型";
        } else if (mType == 3) {
            title = "设备名称";
        } else if (mType == 4) {
            title = "设备材质";
        } else if (mType == 5) {
            title = "行列值";
        } else if (mType == 6) {
            title = "经度";
        } else if (mType == 7) {
            title = "纬度";
        } else if (mType == 8) {
            title = "变电站编码";
        } else if (mType == 9) {
            title = "变压器编码";
        } else if (mType == 10) {
            title = "台区编码";
        } else if (mType == 11) {
            title = "线路编码";
        } else if (mType == 12) {
            title = "用户号";
        }
        mEasyTitle.setTitle(title);

        if (!CheckUtil.isEmpty(mText)) {
            mETContent.setText(mText);
        }
    }

    private void initListener() {
        mTvSave.setOnClickListener(v -> {
            String content = mETContent.getText().toString();
            LiveEventBus.get().with(Constants.EVENT_DEVICE_EDIT).post(new EditBean(mType, content));
            finish();
        });
    }

    @Override
    public void onNetworkConnectionChanged(boolean isConnected, NetWorkUtil.NetworkType networkType) {
    }

    @Override
    protected void dataObserver() {
    }

    private void initView() {
        mEasyTitle = findViewById(R.id.easy_title);
        mETContent = findViewById(R.id.et_content);
        mTvSave = findViewById(R.id.tv_save);
    }
}
